/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cgd;

import cdp.Cliente;
import java.util.Date;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.TemporalType;

/**
 *
 * @author CDIG01
 */
public class DaoJpaCliente extends DaoJpa<Cliente> {
    
    public Cliente buscarEmail(String email) throws Exception {
        try {
            Query q = em.createQuery("SELECT x FROM Cliente x WHERE x.email LIKE '"+email+"'");
            if(q.getResultList().size() == 1)
                return (Cliente)q.getResultList().get(0);
            else
                return null;
        } catch (Exception ex) {
            throw ex;
        }
    }
    
    public Cliente buscarEmailSenha(String email, String senha) throws Exception {
        try {
            Query q = em.createQuery("SELECT x FROM Cliente x WHERE x.email LIKE '"+email+"' AND x.senha LIKE '"+senha+"'");
            if(q.getResultList().size() == 1)
                return (Cliente)q.getResultList().get(0);
            else
                return null;
        } catch (Exception ex) {
            throw ex;
        }
    }
    
    public List<Cliente> buscarPeriodo(Date d1, Date d2) throws Exception {
        try {
            Query q = em.createQuery("SELECT x "
                    + " FROM Cliente x "
                    + " WHERE x.dataCadastro BETWEEN :d1 AND :d2 "
                    + " ORDER BY x.dataCadastro DESC")
                    .setParameter("d1", d1, TemporalType.DATE)
                    .setParameter("d2", d2, TemporalType.DATE);
            return q.getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    }
}
